
package com.dewod.producesales.adapter;

import static com.blankj.utilcode.util.ViewUtils.runOnUiThread;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.dewod.producesales.R;
import com.dewod.producesales.model.request.SnValidate;
import com.dewod.producesales.model.response.PdaSnStatusVo;
import com.dewod.producesales.utils.request.RequestApi4PddUtil;
import com.dewod.producesales.utils.request.RequestApiUtil;
import com.dewod.producesales.viewmodel.SubmitViewModel;
import com.dewod.producesales.viewmodel.ValidateViewModel4Pdd;

import java.util.ArrayList;
import java.util.List;

/**
 * 编码操作栏适应器
 */
public class SnCodeOperaterAdapter extends RecyclerView.Adapter<SnCodeOperaterAdapter.ViewHolder> {

    private List<PdaSnStatusVo> snList;

    private SubmitViewModel viewModel;

    private ValidateViewModel4Pdd validateViewModel;

    private String orderNo;

    public SnCodeOperaterAdapter(SubmitViewModel viewModel, List<PdaSnStatusVo> snList) {
        this.viewModel = viewModel;
        this.snList = snList;
    }

    public SnCodeOperaterAdapter(ValidateViewModel4Pdd viewModel, List<PdaSnStatusVo> snList) {
        this.validateViewModel = viewModel;
        this.snList = snList;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public void updateData(List<PdaSnStatusVo> snList,Integer snType) {
        this.snList = new ArrayList<>(snList);  // 避免引用污染
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sncode_operate, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PdaSnStatusVo sn = snList.get(position);
        holder.tvSnCode.setText(sn.getSn());
        holder.delBtn.setOnClickListener(v -> {
            new AlertDialog.Builder(v.getContext())
                .setTitle("确认删除吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    if(sn.getType() == 2 || sn.getType() == 3 ){ // 快递柜订单业务
                        RequestApiUtil.cancelOne(new SnValidate(orderNo, sn.getSn(), sn.getType()), responseData -> {
                            runOnUiThread(() -> {// 直接在主线程更新
                                if (responseData.getSuccess()) {
                                    // ✅ 安全删除方式
                                    if (sn.getType() == 2) {
                                        viewModel.removeMacSn(sn.getSn());
                                    } else if (sn.getType() == 3) {
                                        viewModel.removeLockSn(sn.getSn());
                                    }
                                } else {
                                    ToastUtils.showLong("删除失败 " + responseData.getMessage());
                                }
                            });
                        });
                    }else if(sn.getType() == 4){ // PDD订单业务
                        RequestApi4PddUtil.cancelOne(new SnValidate(orderNo, sn.getSn(), sn.getType()), responseData -> {
                            runOnUiThread(() -> {// 直接在主线程更新
                                if (responseData.getSuccess()) {
                                    // ✅ 安全删除方式
                                    validateViewModel.removeDeviceSn(sn.getSn());
                                } else {
                                    ToastUtils.showLong("删除失败 " + responseData.getMessage());
                                }
                            });
                        });
                    }

                })
                .setNegativeButton("取消", null)
                .show();
        });
    }

    @Override
    public int getItemCount() {
        return snList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvSnCode;

        ImageButton delBtn;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSnCode = itemView.findViewById(R.id.tv_sn_code);
            delBtn = itemView.findViewById(R.id.btn_delete);
        }
    }
}